package com.demo.ai.springaidemo.chatUtils.garageSearchTool;

import com.alibaba.fastjson.JSON;
import com.demo.ai.springaidemo.param.LocationParams;
import com.demo.ai.springaidemo.utils.HttpClientUtil;
import groovyjarjarantlr4.v4.runtime.misc.NotNull;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.ai.tool.definition.ToolDefinition;
import org.springframework.ai.tool.execution.ToolCallResultConverter;
import org.springframework.ai.tool.function.FunctionToolCallback;
import org.springframework.ai.tool.metadata.ToolMetadata;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.BiFunction;

/**
 * @author wangbo
 * @date 2025/04/02
 */
@Component
public class SearchToolFunction implements ToolCallback{

    @Tool(name = "searchGarage", description = "搜索周围车库")
    public String searchGarage(String body){
        System.out.println("AI调用方法");
        System.out.println(body);
        String url = "http://localhost:10086/user/getGarageMessageRound";
        Map<String, String> headers = Map.of("Content-Type", "application/json");

        String response = HttpClientUtil.sendSyncRequest(url, HttpMethod.POST, headers, body);
        return response;
    }

    @Override
    public ToolDefinition getToolDefinition() {
        ToolDefinition toolDefinition = ToolDefinition.builder()
                .name("searchGarage")
                .inputSchema("""
                        {
                            "type": "string",
                            "properties":{
                                "lon":{
                                    "type": "double"
                                },
                                "lat":{
                                    "type": "double"
                                }
                            },
                            "required": ["lon", "lat"]
                        }
                        """)
                .build();
        return toolDefinition;

    }
    @NotNull
    @Override
    public String call(String toolInput) {
        return searchGarage(toolInput);
    }
}
